package com.sky.service.impl;

import com.sky.context.UserHolder;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {


    @Autowired
    private AddressBookMapper addressBookMapper;


    //查询当前登录用户的所有地址信息
    @Override
    public List<AddressBook> findList(AddressBook addressBook) {
        return addressBookMapper.findList(addressBook);
    }

    //新增地址
    @Override
    public void save(AddressBook addressBook) {
        //1.获取登录人id
        Long userId = UserHolder.get();
        //1-1封装登录人id,调用Service查询
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        //2.调用mapper
        addressBookMapper.save(addressBook);
    }

    //设置默认地址
    @Override
    public void setDefault(AddressBook addressBook) {
        //1.获取登录人id
        Long userId = UserHolder.get();
        //将登录人的所有地址都设置为0,非默认
        addressBookMapper.updateByUserId(userId);


        //把当前地址id设置为默认1
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);

    }

    //修改地址
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.updateAddress(addressBook);
    }

    //删除地址
    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }

    //回显地址//根据收货人的地址id查询收货地址信息
    @Override
    public AddressBook findById(Long id) {
        return addressBookMapper.findById(id);
    }
}
